package com.example.todaynews.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.todaynews.R;
import com.example.todaynews.adapter.NewsContentRecyclerviewAdapter;
import com.example.todaynews.bean.NewsInfo;
import com.example.todaynews.util.ObtainDataUtils;
import com.example.todaynews.view.ListItemDecoration;

import java.util.List;

public class MainActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private Context mContext;
    private static final String TAG = "MainActivity";
    public static final int LOADNEWS_SUCESS = 0;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOADNEWS_SUCESS:
                    final List<NewsInfo> list = (List<NewsInfo>) msg.obj;
                    if (list != null && list.size() > 0) {
                        BindDataForRecyclerView(list);
                    }
                    break;
            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate: 11111111111111111111111111111");
        recyclerView = (RecyclerView) findViewById(R.id.rcv_newscontent);
        initData();
        final List<NewsInfo> list = ObtainDataUtils.getAllNewsFromDatabase(mContext);
        Log.e(TAG, "onCreate: list" + list);
        if (list != null && list.size() > 0) {
            BindDataForRecyclerView(list);
        }
    }

    private void BindDataForRecyclerView(final List<NewsInfo> list) {
        NewsContentRecyclerviewAdapter adapter = new NewsContentRecyclerviewAdapter(mContext, list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new ListItemDecoration(mContext, LinearLayoutManager.VERTICAL));
        adapter.setOnItemClickLitener(new NewsContentRecyclerviewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.e(TAG, "onItemClick:position ==" + position);
                Intent intent = new Intent(mContext, WebActivity.class);
                intent.putExtra("url", list.get(position).getUrl());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Log.e(TAG, "onItemLongClick... ");
            }
        });
    }

    private void initData() {
        mContext = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<NewsInfo> list = ObtainDataUtils.getAllNewsFromNetwork(mContext);
                Log.e(TAG, "initData: list.size() ==" + list.size());
                Message msg = new Message();
                msg.what = LOADNEWS_SUCESS;
                msg.obj = list;
                handler.sendMessage(msg);
            }
        }).start();


    }
}
